#ifndef MCU_H
#define MCU_H
#include <QObject>
#include <QDebug>
#include "Protocol.h"

#define BOOTLOADER_VERSION_SIZE     (8)
#define APP_VERSION_SIZE            (8)
#define MCU_VERSION_SIZE            (BOOTLOADER_VERSION_SIZE  + APP_VERSION_SIZE)


typedef struct {
uint32_t 	operation;///<0-read 1-write other-read
uint32_t 	data;
uint32_t 	addr;
}SensorRegisterOperationPkg;

typedef struct {
    QString mcuAppVersion;
    QString mcuBootVersion;
} McuVersion;

class Mcu : public QObject
{
    Q_OBJECT
public:
    static Mcu* getInstance();
    McuVersion g_mcuVersion;
    void init();
    /* message */
    void systemShutdownMessage();
    void touchSoundBeepMessage();
    void setTouchSoundBeepStateMessage(bool state);
    void setSpiFlashSwitchMessage(uint8_t state);
    void setStripeCalibrationMessage(uint8_t level);
    void setSensorCalibrationMessage(uint8_t level);
    void readMainframeMcuVersionMessage();
    void OPenOrCloseAJL(bool open);
    /* 主机mcu升级指令 */
    void mainframeMcuUpdateJumpMessage();
    void mainframeMcuUpdateStartMessage();
    void mainframeMcuUpdateStopMessage(uint32_t size, uint8_t crc);
    void mainframeMcuUpdateResponseMessage(uint8_t* buffer, uint16_t size);

    /* 其它功能 */
    uint8_t calculateCrc(uint8_t* data, int32_t length);

    /* 其它模块关联接口 */
    void readMainframeMcuVersionAction(Frame *frame);
    void mainframeMcuUpdateRequestAction(Frame *frame);

    void reportSensorState(bool connected);
    void reportSensorImageError();
    void sendSensorRegisterOperation(SensorRegisterOperationPkg &pkg);
private:
    Mcu();

public slots:
    void handle(Frame* frame);

signals:
    void reportKeyValueSignal(uint8_t value);
    void reportMcuVersionSignal(McuVersion* version);
    void mainframeMcuUpdateRequestSignal();
    void reportMcuCurrSensorType(uint8_t value);
    void reportSensorRegisterValue(uint8_t value);
};

#endif // MCU_H
